﻿using SDATA;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace SDLL
{
    public class IGB
    {
        public int OutputLength { get; set; }
        public IGEOutputOrder OutputOrder{get;set;}
        public string SeperatorCharacter { get; set; }
        public bool SeperatorEnabled { get; set; }
        public string StringKey1{get;set;}
        public string StringKey2{get;set;}
        public string StringKey3{get;set;}
        public string TableName { get; set; }
        private string StringKey123{get;set;}
        long intLastKey = 0;
        bool bGenerate;
        string cnn;
        string sql;

        public IGB(string strcnn)
        {
            cnn = strcnn;
        }

        public enum IGEOutputOrder
        {
	        lmNSSS = 0,
	        lmSNSS = 1,
	        lmSSNS = 2,
	        lmSSSN = 3
        }
        public string Generate()
        {
            string s = null;
            string strSeperator = null;
            DataTable dt = new DataTable();
            int intLen = 0;
            this.StringKey123 = StringKey1 + StringKey2 + StringKey3;

            if (TableName == null || TableName == "")
            {
                return "";
            }
            else
            {
                sql = "SELECT * FROM tb0125 WHERE TableName='" + TableName.Trim() + "' AND KeyString= '" + StringKey123 + "'";
                dt = DATA.GetDataTable(sql, cnn);
                if (dt.Rows.Count > 0)
                    intLastKey = long.Parse(dt.Rows[0]["LastKey"].ToString());
                else
                {
                    sql = "INSERT INTO tb0125(TableName,KeyString,LastKey) VALUES('" + TableName + "','" + StringKey123 + "',0)";
                    DATA.ExecuteNonQuery(sql, cnn);
                }
            }
            intLen = OutputLength - (StringKey123.Length + (SeperatorEnabled ? 3 : 0) + intLastKey.ToString().Length);
            if (intLen < 0) { return ""; }
            intLen = OutputLength - (StringKey123.Length + (SeperatorEnabled ? 3 : 0));

            strSeperator = (SeperatorEnabled ? SeperatorCharacter : "");
            switch (OutputOrder)
            {
                case IGEOutputOrder.lmNSSS:
                    s = (intLastKey + 1).ToString().PadLeft(intLen, '0') + strSeperator + StringKey1 + strSeperator + StringKey2 + strSeperator + StringKey3;
                    break;
                case IGEOutputOrder.lmSNSS:
                    s = StringKey1 + strSeperator + (intLastKey + 1).ToString().PadLeft(intLen, '0') + strSeperator + StringKey2 + strSeperator + StringKey3;
                    break;
                case IGEOutputOrder.lmSSNS:
                    s = StringKey1 + strSeperator + StringKey2 + strSeperator + (intLastKey + 1).ToString().PadLeft(intLen, '0') + strSeperator + StringKey3;
                    break;
                case IGEOutputOrder.lmSSSN:
                    s = StringKey1 + strSeperator + StringKey2 + strSeperator + StringKey3 + strSeperator + (intLastKey + 1).ToString().PadLeft(intLen, '0');
                    break;
            }
            bGenerate = true;
            return s;
        }

        public long GetLastKey()
        {
            DataTable dt = new DataTable();
            StringKey123 = StringKey1 + StringKey2 + StringKey3;
            if (TableName != "" && StringKey123 != "")
            {
                sql = "SELECT * FROM tb0125 WHERE TableName='" + TableName.Trim() + "' And KeyString='" + StringKey123 + "'";
                dt = DATA.GetDataTable(sql, cnn);
                if (dt.Rows.Count > 0)
                    return long.Parse(dt.Rows[0]["LastKey"].ToString());
            }
            return 0;
        }

        public bool SaveLastKey(long nNewLastKey)
        {
            StringKey123 = StringKey1 + StringKey2 + StringKey3;
            if (TableName != "" && StringKey123 != "")
            {
                sql = "UPDATE tb0125 Set LastKey=" + nNewLastKey + " WHERE TableName='" + TableName + "' And KeyString='" + StringKey123 + "'";
                return DATA.ExecuteNonQuery(sql,cnn);
            }
            return false;
        }

        public bool SaveNewKey()
        {
            StringKey123 = StringKey1 + StringKey2 + StringKey3;
            if (!bGenerate) { return false; }
            sql = "UPDATE tb0125 Set LastKey=" + (intLastKey + 1).ToString() + " WHERE TableName='" + TableName + "' And KeyString='" + StringKey123 + "'";
            return DATA.ExecuteNonQuery(sql, cnn);
        }


    }
}
